package cn.org.timespace.easyframe.action.permission;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.easyframe.action.EfLogonAction;
import cn.org.timespace.easyframe.model.permission.EfOrg;
import cn.org.timespace.easyframe.service.permission.EfOrgService;
import cn.org.timespace.easyframe.util.generator.IdentityGenerator;

public class EfOrgAction extends EFBaseAction{
	
/**
* <p>
* 机构管理
* </p>
* 
* @version 1.0, 11/02/11
* @since 1.0
* @author wubo
*/ 
	
	private static final long serialVersionUID = -6620804881445347808L;
	private static Log log = LogFactory.getLog(EfLogonAction.class);
	
	private EfOrgService efOrgService;

	// 构建模型驱动bean
	public EfOrgAction() {
		super(new EfOrgModel());
	}

	EfOrgModel efOrgModel = (EfOrgModel) this.getModel();

	public EfOrgService getEfOrgService() {
		return efOrgService; 
	}

	public void setEfOrgService(EfOrgService efOrgService) {
		this.efOrgService = efOrgService;
	}

	public String doList()  throws Exception{
		
		List<EfOrg> allOrgList = efOrgService.findAll();
		getRequest().setAttribute(VOLIST, allOrgList);

		return LIST;
		
	}
	
	//添加机构
	public String doSave()  throws Exception{
		//获取主键
		String orgId=IdentityGenerator.getInstance().getId();
		EfOrg efOrg=efOrgModel.getEfOrg();
		efOrg.setOrgId(orgId);
		String sheng =	efOrg.getSelA();//省
	    String shi =	efOrg.getSelB();//市
	    String qu = efOrg.getSelC();//县/区
	    String id = "";
	    if(qu!=null&&qu.length()>0){
	    	 id = qu;
	    }else if(shi!=null&&shi.length()>0){
	    	
	    	 id = shi;	
	    }else if(sheng!=null&&sheng.length()>0){
	    	 id = sheng;
	    }
	    if(id!=null&&id.length()>0){
	    	efOrg.setOrgCode(id);
	    }
		efOrgService.save(efOrg);
		return UPDATE;
	}
	
	//修改
	public String doUpdate()  throws Exception{
		
		EfOrg efOrg = efOrgModel.getEfOrg();
		String sheng =	efOrg.getSelA();//省
	    String shi =	efOrg.getSelB();//市
	    String qu = efOrg.getSelC();//县/区
	    String id = "";
	    if(qu!=null&&qu.length()>0){
	    	 id = qu;
	    }else if(shi!=null&&shi.length()>0){
	    	
	    	 id = shi;	
	    }else if(sheng!=null&&sheng.length()>0){
	    	 id = sheng;
	    }
	    if(id!=null&&id.length()>0){
	    	if(!efOrg.getOrgCode().equals(id)){
	    		efOrg.setOrgCode(id);
	    	}
	    	
	    }
		efOrgService.update(efOrg);
		
		return UPDATE;
	}
	
	//根据机构Id获得机构对象
	public String doFindById() throws Exception
	{
		//更新
		String orgId=getRequest().getParameter("orgId");	
		if(!StringUtils.isEmpty(orgId)){ 
			EfOrg efOrg=efOrgService.findById(orgId);
			efOrgModel.setEfOrg(efOrg);
			return UPDATE;
		}
		
		//增加
		String parentMenuId=getRequest().getParameter("parentId");	
		if(!StringUtils.isEmpty(parentMenuId)){ 
			EfOrg efOrg=new EfOrg();
			efOrg.setParentId(parentMenuId);
			efOrgModel.setEfOrg(efOrg);
			return ADD;
		}
		
		return null;
	}
	
	//删除
	public String doDelete()  throws Exception{
		String orgId=getRequest().getParameter("orgId");
		EfOrg efOrg=efOrgService.findById(orgId);
		efOrgService.delete(efOrg);
		return null;
	}

}
